/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ArticleEditor.OptionsView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JToolTip;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.articleEditor.view.Styleable;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;
import org.openide.util.Utilities;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//org.ArticleEditor.OptionsView//MenuOptions//EN",
        autostore = false
)
@TopComponent.Description(
        preferredID = "MenuOptionsTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "org.ArticleEditor.OptionsView.MenuOptionsTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_MenuOptionsAction",
        preferredID = "MenuOptionsTopComponent"
)
@Messages({
    "CTL_MenuOptionsAction=MenuOptions",
    "CTL_MenuOptionsTopComponent=Menu Opciones",
    "HINT_MenuOptionsTopComponent=This is a Menu Options"
})
public final class MenuOptionsTopComponent extends TopComponent implements LookupListener{
    
    DataObject Dobj;
    private Styleable styleable;
    private Lookup.Result<DataObject> result = null;
    private Lookup.Result<Styleable> resultStyle = null;
    
/**
 * Contructor del TopComponent MenuOptions
 */  
    public MenuOptionsTopComponent() {
        initComponents();
        setName(Bundle.CTL_MenuOptionsTopComponent());
        setToolTipText(Bundle.HINT_MenuOptionsTopComponent());
        Dobj = null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        JBtnTitulo = new javax.swing.JButton();
        jBtnAutores = new javax.swing.JButton();
        jBtnResumen = new javax.swing.JButton();
        jBtnPalabrasClave = new javax.swing.JButton();
        jBtnAbstract = new javax.swing.JButton();
        jBtnKeyWords = new javax.swing.JButton();
        jBtnIntroduccion = new javax.swing.JButton();
        jBtnMatMetodos = new javax.swing.JButton();
        jBtnResultados = new javax.swing.JButton();
        jBtnDiscusion = new javax.swing.JButton();
        jBtnConclusiones = new javax.swing.JButton();
        jBtnAgradecimientos = new javax.swing.JButton();
        jBtnConflictos = new javax.swing.JButton();
        jBtnBibliografia = new javax.swing.JButton();
        jBtnComentarios = new javax.swing.JButton();

        org.openide.awt.Mnemonics.setLocalizedText(JBtnTitulo, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.JBtnTitulo.text")); // NOI18N
        JBtnTitulo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBtnTituloActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnAutores, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnAutores.text")); // NOI18N
        jBtnAutores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAutoresActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnResumen, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnResumen.text")); // NOI18N
        jBtnResumen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnResumenActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnPalabrasClave, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnPalabrasClave.text")); // NOI18N
        jBtnPalabrasClave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnPalabrasClaveActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnAbstract, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnAbstract.text")); // NOI18N
        jBtnAbstract.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAbstractActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnKeyWords, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnKeyWords.text")); // NOI18N
        jBtnKeyWords.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnKeyWordsActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnIntroduccion, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnIntroduccion.text")); // NOI18N
        jBtnIntroduccion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnIntroduccionActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnMatMetodos, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnMatMetodos.text")); // NOI18N
        jBtnMatMetodos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnMatMetodosActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnResultados, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnResultados.text")); // NOI18N
        jBtnResultados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnResultadosActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnDiscusion, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnDiscusion.text")); // NOI18N
        jBtnDiscusion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnDiscusionActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnConclusiones, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnConclusiones.text")); // NOI18N
        jBtnConclusiones.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnConclusionesActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnAgradecimientos, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnAgradecimientos.text")); // NOI18N
        jBtnAgradecimientos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAgradecimientosActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnConflictos, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnConflictos.text")); // NOI18N
        jBtnConflictos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnConflictosActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jBtnBibliografia, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnBibliografia.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jBtnComentarios, org.openide.util.NbBundle.getMessage(MenuOptionsTopComponent.class, "MenuOptionsTopComponent.jBtnComentarios.text")); // NOI18N
        jBtnComentarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnComentariosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jBtnMatMetodos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnIntroduccion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnKeyWords, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnAbstract, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnPalabrasClave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnResumen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnAutores, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(JBtnTitulo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnResultados, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnDiscusion, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnConclusiones, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnAgradecimientos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnConflictos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnBibliografia, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jBtnComentarios, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(JBtnTitulo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnAutores)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnResumen)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnPalabrasClave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnAbstract)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnKeyWords)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnIntroduccion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnMatMetodos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnResultados)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnDiscusion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnConclusiones)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnAgradecimientos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnConflictos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jBtnBibliografia)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBtnComentarios)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBtnComentariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnComentariosActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jBtnComentariosActionPerformed

/**
 * Metodo que implementa la lógica del Botón Título
 * @param evt 
 */
    private void JBtnTituloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBtnTituloActionPerformed
     
        String Texto;
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");            
        }
        else{  
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    for(int i = 0; i<4; i++){
                        int tam = Document.getParagraphArray(i).getText().length();                        
                        if(tam >= 5){
                            Texto = Document.getParagraphArray(i).getText();
                            styleable.selected(tam, i);
                            i=4;
                        }
                    }                                                            
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }                                                        
        }     
    }//GEN-LAST:event_JBtnTituloActionPerformed
/**
 * Metodo que implementa la lógica del Botón Autores
 * @param evt 
 */
    private void jBtnAutoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAutoresActionPerformed
        if(Dobj == null){            
            this.setToolTipText("El articulo no contiene autores.");            
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    int aux = 0;
                    int Long = 0;
                    for(int i = 0; i<4; i++){
                        int tam = Document.getParagraphArray(i).getText().length();
                        System.out.println("TAMAÑOOOOOOOOOOOOO... "+tam + " # Parrafo: "+i);
                        if(tam >= 5){
                            aux = aux +1;
                            Long = Long + tam; 
                            if(aux == 2){
                                Long = Long +1;
                                styleable.selected(Long, i);
                                i=4;
                            }                                                                                  
                        }
                        else{
                            Long = Long + tam;
                        }
                    }                                                            
                }
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }
        }
    }//GEN-LAST:event_jBtnAutoresActionPerformed
/**
 * Metodo que implementa la lógica del Botón Resumen
 * @param evt 
 */
    private void jBtnResumenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnResumenActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector resumen = new Vector();
                    resumen.add(0, "resumen");
                    resumen.add(1, "resumen:");
                    int posicion = getPosWord(resumen, getNumParagraphs(Document), Document);
                    if(posicion == -1){
                        //JOptionPane.showConfirmDialog(jBtnAutores, "Su artículo no presenta resumen, le gustaría crear uno?", "Confirmar Resumen", , JOptionPane.PLAIN_MESSAGE)
                    }
                    else{
                        System.out.println("POSICION RESUMEN" + posicion);
                        styleable.selected(posicion, posicion);
                    }
                    
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }                                   
        }        
        
    }//GEN-LAST:event_jBtnResumenActionPerformed
/**
 * Metodo que implementa la lógica del Botón Palabras Clave
 * @param evt 
 */
    private void jBtnPalabrasClaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnPalabrasClaveActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    JToolTip tool = this.createToolTip();
                    Vector palabrasC = new Vector();
                    palabrasC.add(0, "palabras clave");
                    palabrasC.add(1, "palabras clave:");
                    int posicion = getPosWord(palabrasC, getNumParagraphs(Document), Document);
                    System.out.println("posicion#######" +posicion);
                    if (posicion == -1){
                        JToolTip toolTip = new JToolTip();
                        toolTip.setTipText("Press Enter to insert \"" +  "\"");
                        
                        
                       // jBtnPalabrasClave.setToolTip();                        
                    }
                    else{
                        System.out.println("POSICION RESUMEN" + posicion);
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }           
            
            
        }
    }//GEN-LAST:event_jBtnPalabrasClaveActionPerformed
/**
 * Metodo que implementa la lógica del Botón Abstract
 * @param evt 
 */
    private void jBtnAbstractActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAbstractActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector Abstract = new Vector();
                    Abstract.add(0, "abstract");
                    Abstract.add(1, "abstract:");
                    int posicion = getPosWord(Abstract, getNumParagraphs(Document), Document);
                    if(posicion == -1){
                    
                    }
                    else{
                        styleable.selected(posicion, posicion);
                     }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }           
            
            
        }
    }//GEN-LAST:event_jBtnAbstractActionPerformed
/**
 * Metodo que implementa la lógica del Botón Key Words
 * @param evt 
 */
    private void jBtnKeyWordsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnKeyWordsActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(jBtnAutores, result);
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector keyWords = new Vector();
                    keyWords.add(0, "key words");
                    keyWords.add(1, "key words:");
                    int posicion = getPosWord(keyWords, getNumParagraphs(Document), Document); 
                    if(posicion == -1){
                    
                    }
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }           
            
            
        }
    }//GEN-LAST:event_jBtnKeyWordsActionPerformed
/**
 * Metodo que implementa la lógica del Botón Introducción
 * @param evt 
 */
    private void jBtnIntroduccionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnIntroduccionActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector introduccion = new Vector();
                    introduccion.add(0, "introducción");
                    introduccion.add(1, "introducción:");
                    introduccion.add(2, "introduccion:");
                    introduccion.add(3, "introduccion");
                    int posicion = getPosWord(introduccion, getNumParagraphs(Document), Document); 
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }           
            
            
        }
    }//GEN-LAST:event_jBtnIntroduccionActionPerformed
/**
 * Metodo que implementa la lógica del Botón Materiales y Métodos
 * @param evt 
 */
    private void jBtnMatMetodosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnMatMetodosActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector materialesMet = new Vector();
                    materialesMet.add(0, "materiales y métodos");
                    materialesMet.add(1, "materiales y métodos:");
                    materialesMet.add(2, "materiales y metodos:");
                    materialesMet.add(3, "materiales y metodos");
                    int posicion = getPosWord(materialesMet, getNumParagraphs(Document), Document); 
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }           
            
            
        }
    }//GEN-LAST:event_jBtnMatMetodosActionPerformed
/**
 * Metodo que implementa la lógica del Botón Resultados
 * @param evt 
 */
    private void jBtnResultadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnResultadosActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector resultados = new Vector();
                    resultados.add(0, "resultados");
                    resultados.add(1, "resultados:");
                   
                    int posicion = getPosWord(resultados, getNumParagraphs(Document), Document);  
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }                                   
        }
    }//GEN-LAST:event_jBtnResultadosActionPerformed
/**
 * Metodo que implementa la lógica del Botón Discusión
 * @param evt 
 */
    private void jBtnDiscusionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnDiscusionActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector discusion = new Vector();
                    discusion.add(0, "discusión");
                    discusion.add(1, "discusión:");
                    discusion.add(2, "discusion:");
                    discusion.add(3, "discusion");
                    int posicion = getPosWord(discusion, getNumParagraphs(Document), Document); 
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }             
        }
    }//GEN-LAST:event_jBtnDiscusionActionPerformed
/**
 * Metodo que implementa la lógica del Botón Conclusiones
 * @param evt 
 */
    private void jBtnConclusionesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnConclusionesActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector conclusiones = new Vector();
                    conclusiones.add(0, "conclusiones");
                    conclusiones.add(1, "conclusiones:");
                    int posicion = getPosWord(conclusiones, getNumParagraphs(Document), Document);  
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }  
        }
    }//GEN-LAST:event_jBtnConclusionesActionPerformed
/**
 * Metodo que implementa la lógica del Botón Agradecimientos
 * @param evt 
 */
    private void jBtnAgradecimientosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAgradecimientosActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector agradecimientos = new Vector();
                    agradecimientos.add(0, "agradecimientos");
                    agradecimientos.add(1, "agradecimientos:");
                    
                    int posicion = getPosWord(agradecimientos, getNumParagraphs(Document), Document);      
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            } 
        }
    }//GEN-LAST:event_jBtnAgradecimientosActionPerformed
/**
 * Metodo que implementa la lógica del Botón Conflictos de interes
 * @param evt 
 */
    private void jBtnConflictosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnConflictosActionPerformed
        if(Dobj == null){
            JOptionPane.showMessageDialog(JBtnTitulo, "Seleccione un archivo");    
        }
        else{
            try {
                XWPFDocument Document = getDocument(Dobj);
                if(getNumParagraphs(Document) == 1){
                    styleable.selected(0,0);
                }
                else{
                    Vector conflictos = new Vector();
                    conflictos.add(0, "conflictos de interes");
                    conflictos.add(1, "conflictos de interes:");
                    int posicion = getPosWord(conflictos, getNumParagraphs(Document), Document);                    
                    if(posicion == -1){}
                    else{
                        styleable.selected(posicion, posicion);
                    }
                }
                    
            } catch (IOException ex) {
                Exceptions.printStackTrace(ex);
            }     
        }
    }//GEN-LAST:event_jBtnConflictosActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBtnTitulo;
    private javax.swing.JButton jBtnAbstract;
    private javax.swing.JButton jBtnAgradecimientos;
    private javax.swing.JButton jBtnAutores;
    private javax.swing.JButton jBtnBibliografia;
    private javax.swing.JButton jBtnComentarios;
    private javax.swing.JButton jBtnConclusiones;
    private javax.swing.JButton jBtnConflictos;
    private javax.swing.JButton jBtnDiscusion;
    private javax.swing.JButton jBtnIntroduccion;
    private javax.swing.JButton jBtnKeyWords;
    private javax.swing.JButton jBtnMatMetodos;
    private javax.swing.JButton jBtnPalabrasClave;
    private javax.swing.JButton jBtnResultados;
    private javax.swing.JButton jBtnResumen;
    // End of variables declaration//GEN-END:variables
   
    
    /**
     * 
     */
    @Override
    public void componentOpened() {
        result = Utilities.actionsGlobalContext().lookupResult(DataObject.class);
        resultStyle = Utilities.actionsGlobalContext().lookupResult(Styleable.class);
        result.addLookupListener (this);
        resultStyle.addLookupListener(this);
        
    }

    @Override
    public void componentClosed() {       
        result.removeLookupListener(this);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent le) {
        Collection<? extends DataObject> allEvents = result.allInstances();
        Collection<? extends Styleable> allEventsStyle = resultStyle.allInstances();
        if (!allEvents.isEmpty()) {
            DataObject event = allEvents.iterator().next();            
            Dobj = event;
                if(!allEventsStyle.isEmpty()){
                    System.out.println("Styleable no está vacio");
                    styleable = allEventsStyle.iterator().next();
                }
            
        } else {
            System.out.println("--");
        }
    }
    
    public int getNumParagraphs(XWPFDocument document) throws FileNotFoundException, IOException{          
        List<XWPFParagraph> parrafos =  document.getParagraphs();
        return parrafos.size();
    }
    
    public XWPFDocument getDocument(DataObject dataObject) throws FileNotFoundException, IOException{
        org.openide.filesystems.FileObject documentFileObject = dataObject.getPrimaryFile();
        File documentFile = FileUtil.toFile(documentFileObject);
        FileInputStream docxIS;
        docxIS = new FileInputStream(documentFile);
        XWPFDocument document = new XWPFDocument(docxIS); 
        return document;
    }
    
    public int getPosWord(Vector words, int NumParagraphs, XWPFDocument Document){
        int Pos = -1;
        int i;
        if(NumParagraphs == 1 && Document.getParagraphArray(0).equals("")){
            return Pos;
        }
        else{
           Pos = 0;
            for( i = 0; i < NumParagraphs; i++){
                XWPFParagraph parrafo = Document.getParagraphArray(i);                
                String Parrafo = parrafo.getText().toLowerCase();
                int longitud = words.get(0).toString().length()+1;
                if(isWord(words, Parrafo) == true && parrafo.getText().length() <= longitud ){ 
                        Pos = Pos + parrafo.getText().length();
                        i = NumParagraphs;                       
                }
                else{
                    Pos = Pos + parrafo.getText().length()+1;
                }            
            }
            if(i == NumParagraphs){
                return -1;
            }
            else{
                return Pos;
            }
        }
    }
    
    public boolean isWord(Vector words, String paragraph){
        int t = words.size();
        boolean confirm = false;
        for(int i=0; i<t ; i++){
            if(words.get(i).equals(paragraph.toLowerCase())){
                confirm = true;
                i = t;
            }
        }
        return confirm;
    }
}